//求根节点到叶节点数字之和
//测试链接：https://leetcode.cn/problems/sum-root-to-leaf-numbers/
public class SumNumbers {

    public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode() {}
       TreeNode(int val) { this.val = val; }
       TreeNode(int val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
       }
   }

    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }

    public int dfs(TreeNode root, int  preSum) {
        preSum = preSum * 10 + root.val;
        if(root.left == null && root.right == null) return preSum;

        int ret = 0;
        if(root.left != null) ret += dfs(root.left, preSum);
        if(root.right != null) ret += dfs(root.right, preSum);

        return ret;
    }
}
